//-------------------------------------------------------------------
// PROYECTO: A Pack of Classic Games For BennuGD.
// AUTORES: DCelso, TYCO, Kim-ele-to, PiXeL, PRG, SplinterGU
// FECHA INICIO: 07/11/2008.
// FECHA FINALIZACION: xx/xx/xxxx.
// REVISION: 19/10/2009.
// VERSION PARA: Bennu WIP-15c2 y superiores.
//-------------------------------------------------------------------

PROGRAM apagame4be; // A PAck of classic GAMEs for BEnnugd

import "mod_grproc"
import "mod_proc"
import "mod_dir"
import "mod_sys"
import "mod_wm"
import "mod_rand"
import "mod_video"
import "mod_mem"
import "mod_text"
import "mod_map"

include "commons/global.prg"
include "commons/keys_control.prg"

//******************************************************************************************************
// CODIGO INICIAL DEL PROGRAMA
//******************************************************************************************************
BEGIN
    initialice_game();
    set_title("A Pack of classic Games For BennuGD v" + VERSION);
   
    menu();
END

//******************************************************************************************************
// PROCESO QUE GENERA EL MENU
//******************************************************************************************************
PROCESS menu ()

PRIVATE
    int i;
    int option=0;
    int textsTemp[100];
    int num_option;
    string * game_name;
    int selected=0;

    // CONTROL DE TIEMPO DE TECLA PULSADA
    int counter_time;
    int key_press;

    String currdir;
    string args[10];
    int ret;
BEGIN
    signal(father,s_kill);
    
    while(glob("games/*.*") != "")
        if(fileinfo.directory && fileinfo.name != "." && fileinfo.name != ".." && fileinfo.name != ".svn");
            game_name = realloc(game_name, ( num_option + 1 ) * sizeof ( *game_name ));
            game_name[num_option++] = fileinfo.name;
        end
    end
    game_name = realloc(game_name, ( num_option + 1 ) * sizeof ( *game_name ));
    game_name[num_option++] = "Salir";
      
    For (i=0;i<num_option;i++)
        textsTemp[i]=write(0,SCREEN_WIDTH/2,50+(i*10),4,game_name[i]);
    End
	write(0,SCREEN_WIDTH/2,SCREEN_HEIGHT-30,4,"Pulsa F para jugar en pantalla completa");
	write(0,SCREEN_WIDTH/2,SCREEN_HEIGHT-20,4,"Pulsa W para jugar en una ventana");
    selected = selected_line(SCREEN_WIDTH/2,50);
    
    While (!is_key_pressed(PLAYER_1,KEY_CLOSE))
		If (key(_f)) full_screen=true; set_mode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_DEPTH); End
		If (key(_w)) full_screen=false; set_mode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_DEPTH); End 

        // Si la tecla no es START SOLO MIRAMOS EL TECLADO CADA 3 FRAMES o al soltar la tecla
        counter_time = 0;
        key_press = scan_code;
        if (!is_key_pressed(PLAYER_1,KEY_START))
		    while (scan_code == key_press AND counter_time <3 )
		    	counter_time++;
		    	frame;
		    end
		end

        // COMPROBAMOS QUE TECLA HEMOS PULSADO
        if (is_key_pressed(PLAYER_1,KEY_UP))
            option--;
            selected.y-=10;
            If (option < 0)
                option=0;
                selected.y+=10;
            End
        elsif (is_key_pressed(PLAYER_1,KEY_DOWN))
            option++;
            selected.y+=10;
            If (option >= num_option)
                option=num_option-1;
                selected.y-=10;
            End
        elsif (is_key_pressed(PLAYER_1,KEY_CLOSE))
            exit("Gracias por Jugar a A Pack of Classic Games For Bennu.",0);
        elsif (is_key_pressed(PLAYER_1,KEY_START))
	        // esperamos a soltar la tecla
	        while (scan_code != 0) frame;end
		    if ( option == num_option - 1 )
                exit("Gracias por Jugar a A Pack of Classic Games For Bennu.",0);
            end
            //signal(id, S_SLEEP_TREE);
            //signal(id, S_WAKEUP);
            currdir = cd();
            chdir ("games/"+ game_name[option] );
            args[0] = game_name[option];
            ret = exec( _P_WAIT, "bgdi", 1, &args );
            chdir (currdir);
            //signal(id, S_WAKEUP_TREE);
        End            
        Frame;
    End
    let_me_alone();
END

//******************************************************************************************************
// PROCESO QUE MUESTRA EL JUEGO SELECCIONADO
//******************************************************************************************************
PROCESS selected_line (int x, int y)
BEGIN
    graph = new_map(200,10,SCREEN_DEPTH);
    map_clear(0,graph,rgb(0,128,0));
    While(!is_key_pressed(PLAYER_1,KEY_CLOSE))
        Frame;
    End
    unload_map(0,graph);
END
